<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of NetworkController
 *
 * @author Dennis
 */
class Admin_NetworkController extends Zend_Controller_Action {

    protected $_netwerk;

    public function init() {
        $this->view->url = $this->_helper->mvcUrl();
        $this->_netwerk = new Default_Model_DbTable_Netwerkstatus();
    }

    public function indexAction() {
        $this->view->netwerk = $this->_netwerk->fetchAll($this->_netwerk->select()->from($this->_netwerk, array('id', 'header', 'message')))->toArray();
    }

    public function addAction() {
        $form = $this->_helper->form('netwerk');
        $this->view->form = $form;
        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {

                $values = $form->getValues();
                $data = array(
                        'id' => null,
                        'header' => $values['netwerkHeader'],
                        'message' => $values['netwerkMessage'],
                        'startDate' => $values['startDatum'],
                        'endDate' => $values['eindDatum'],
                        'owner' => Zend_Auth::getInstance()->getStorage()->read()->id
                );
                $this->_netwerk->insert($data);
                return $this->_helper->redirector('index');
            }

        }
    }

    public function editAction() {
        $form = $this->_helper->form('netwerk');
        $form->setAction($this->view->url['edit']."/id/".$this->getRequest()->getParam('id'));


        $message = $this->_netwerk->find($this->getRequest()->getParam('id'));
        $message = $message[0];

        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {

                $values = $form->getValues();
                $data = array(
                        'header' => $values['netwerkHeader'],
                        'message' => $values['netwerkMessage'],
                        'startDate' => $values['startDatum'],
                        'endDate' => $values['eindDatum'],
                );

                $this->_netwerk->update($data, $this->_netwerk->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
                return $this->_helper->redirector('index');
            }

        }else{
            $element = $form->getelement('netwerkHeader');
            $element->setValue($message['header']);
            $element = $form->getelement('netwerkMessage');
            $element->setValue($message['message']);
            $element = $form->getelement('startDatum');
            $element->setValue($message['startDate']);
            $element = $form->getelement('eindDatum');
            $element->setValue($message['endDate']);

        }

        $this->view->form = $form;
    }

    public function deleteAction() {
        $this->_netwerk->delete($this->_netwerk->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
        return $this->_helper->redirector('index');
    }

}
?>
